import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { DatePipe } from '@angular/common';
import { differenceInCalendarDays } from 'date-fns';

@Component({
  selector: 'app-recipe-data',
  templateUrl: './recipe-data.component.html',
  styleUrls: ['./recipe-data.component.scss'],
  providers: [
    DatePipe
  ]
})
export class RecipeDataComponent implements OnInit {
  today = new Date();
  currentMonth = `${new Date().getFullYear()}-${new Date().getMonth() + 1}-1`;
  searchParams = {
    start_date: this.datePipe.transform(this.currentMonth, 'yyyy-MM-dd HH:mm:ss'),
    end_date: this.datePipe.transform(this.today, 'yyyy-MM-dd HH:mm:ss'),
    doctor: '全部',
    department: '全部',
    type: '全部'
  };
  anySearchRes = {
    start_date: this.datePipe.transform(this.currentMonth, 'yyyy-MM-dd HH:mm:ss'),
    end_date: this.datePipe.transform(this.today, 'yyyy-MM-dd HH:mm:ss'),
    doctor: '全部',
    department: '全部',
    type: '全部'
  };
  resultsObj = null;
  keshiOptions: any = [];
  yishengOptions: any = [];
  moreGongxiaoTableData = true;
  gongxiaoTableData: any = {};
  constructor(public http: HttpClient, public datePipe: DatePipe) { }
  disabledDate = (current: Date): boolean => {
    // Can not select days before today and today
    return differenceInCalendarDays(current, this.searchParams['start_date']) < 0 || differenceInCalendarDays(current, this.searchParams['start_date']) === 0;
  }

  /**
   * @description 搜索方法
   */
  onSubmit() {
    this.searchParams.start_date = this.datePipe.transform(this.searchParams['start_date'], 'yyyy-MM-dd HH:mm:ss');
    this.searchParams.end_date = this.datePipe.transform(this.searchParams['end_date'], 'yyyy-MM-dd HH:mm:ss');
    this.anySearchRes = JSON.parse(JSON.stringify(this.searchParams));
    const params = {
      params: this.searchParams
    };
    this.http.get(window.API_ROOT + 'api/statistics/associated_recipe', params).toPromise().then((res: any) => {
      console.log(res);
      this.resultsObj = res.data;
      this.getGongxiaofenxi();
    });
  }

  /**
  * @description 获取科室
  */
 getKeshi() {
  const params = {
    params: {
      value: ''
    }
  };
  this.http.get(window.API_ROOT + 'api/recipes/departments', params).toPromise().then((res: any) => {
    console.log(res);
    this.keshiOptions = res.data;
  });
}
/**
* @description 获取医生
*/
getYisheng() {
  const params = {
    params: {
      '科室': this.searchParams['department']
    }
  };
  this.http.get(window.API_ROOT + 'api/recipes/doctors', params).toPromise().then((res: any) => {
    console.log(res);
    this.yishengOptions = res.data;
  });
}



    /**
   * @description 大处方科室分布表格
   */
  getGongxiaofenxi() {
    this.moreGongxiaoTableData = !this.moreGongxiaoTableData;
    if (!this.moreGongxiaoTableData) {
      this.gongxiaoTableData['head'] = JSON.parse(JSON.stringify(this.resultsObj['head']));
      this.gongxiaoTableData['body'] = JSON.parse(JSON.stringify(this.resultsObj['body'].slice(0, 5)));
    } else {
      this.gongxiaoTableData['head'] = JSON.parse(JSON.stringify(this.resultsObj['head']));
      this.gongxiaoTableData['body'] = JSON.parse(JSON.stringify(this.resultsObj['body']));
    }
  }

  ngOnInit() {
    this.getKeshi();
    this.getYisheng();
    this.onSubmit();
  }

}
